<?php

class LocationcontactsController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
         
                'users'=>array('@'),
            ),
         
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

  

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {

        $model = new Locationcontacts;
        
        if(isset($_POST['Locationcontacts'])){
            $model->attributes = $_POST['Locationcontacts'];
            if($model->save()){
                $this->showAjaxMessage('',true);
            }else{
                $this->showAjaxMessage('Can not save data',false);
            }
        }
                            
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if(isset($_POST['Locationcontacts'])){
            $model->attributes = $_POST['Locationcontacts'];
            if($model->save()){
                $this->showAjaxMessage('',true);
            }else{
                $this->showAjaxMessage('Can not save data',false);
            }
        }
        
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
            
            $model=$this->loadModel($id);
            $id=Yii::app()->db->createCommand('select * from tbl_locations where custno=:custno AND locno=:locno')->queryScalar(array(
                'custno'=>$model->custno,
                'locno'=>$model->locno,
            ));
            $model->status = Locationcontacts::CONTACT_STATUS_DELETED;
            $model->save();
            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(array('index','id'=>$id));
   
    }

    /**
     * Lists all models.
     */
    public function actionIndex($id) {
       
        $location=  Locations::model()->find('id=:id',array('id'=>$id));
        $customer=Customers::model()->find('custno=:custno',array(
            'custno'=>$location->custno,
        ));
        $statusData= (Locationcontacts::model()->getStatusList());
        outputJsByList($statusData,'statusData',false);
        $this->render('index', array(
            'customer'=>$customer,
            'location'=>$location,
        ));
    }

  

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = Locationcontacts::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'locationcontacts-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    public function actionAjaxView($id){
      
        $rs=$this->loadModel($id)->getAttributes();
        echo json_encode($rs);
        Yii::app()->end();
      
    }
    
   
    /*
     * How to implement custno and locno
     */
    public function actionGetLocationcontactsList($custno=NULL,$locno=NULL,$status = Locationcontacts::CONTACT_STATUS_ACTIVE,$query=NULL,$page=1,$sort=NULL,$dir=NULL){
        //we decrease $page by 1 because in Extjs first page is 1, but in our sql first page is 0;
        if($page>0)$page--;
        $order=CJSON::decode($sort);
        $criteria=new CDbCriteria();
        if($sort!=NULL){
            $criteria->order=$sort!=NULL?$order[0]['property'].' '.$order[0]['direction']:'locno ASC';
        }
        if($custno!=NULL && $locno!=NULL){
            $criteria->compare('custno', $custno);
             $criteria->addInCondition('locno', array($locno,'00000'));
             $criteria->order = 'locno';
        }
        $criteria->compare('status', $status);
        $contacts=new CActiveDataProvider('Locationcontacts',array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>20,
                'currentPage'=>$page,
            )
        ));
        $rs=array();
        if(count($contacts->getData())>0){
            $rs['totalCount']=$contacts->getTotalItemCount();
            $rs['type']='Locationcontacts';
            foreach($contacts->getData() as $item){
                $rs['data'][]=$item->getAttributes();
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
    
}
